/**
 * 
 */
package com.wsd.action;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wsd.bean.OrderList;
import com.wsd.bean.ProductOrder;
import com.wsd.bean.Users;
import com.wsd.service.ProductOrderService;

/**
 * @author TuZiJun
 *
 */
public class OrderAction extends ActionSupport {
	private int pid;
	private int pnum;
	private String ps;
	private ProductOrderService productOrderService = null;
	public void setProductOrderService(ProductOrderService productOrderService) {
		this.productOrderService = productOrderService;
	}
	public void setPnum(int pnum) {
		this.pnum = pnum;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public void setPs(String ps) {
		this.ps = ps;
	}
	@SuppressWarnings("unchecked")
	public String displayProduct() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Set<OrderList> orderlist = (Set<OrderList>) session.getAttribute("orderlist");
		Users u = (Users) session.getAttribute("user");
		if (!u.isState()) {
			request.setAttribute("msg", "您的账号当前正在审核中。。。");
			return SUCCESS;
		}
		if (null == orderlist || orderlist.size() == 0)
			request.setAttribute("msg", "订单中没有任何产品，请选购喜欢的产品加入订单中。");
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addProduct() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Users u = (Users) session.getAttribute("user");
		if (null == u)
			return LOGIN;
		if (!u.isState()) {
			request.setAttribute("msg", "您的账号当前正在审核中。。。");
			return SUCCESS;
		}
		Set<OrderList> orderlist = (Set<OrderList>) session.getAttribute("orderlist");
		if (null == orderlist){
			orderlist = new HashSet<OrderList>();
		}
		orderlist = productOrderService .saveProduct(orderlist, pnum, pid, u.getUserLevel());
		session.setAttribute("orderlist", orderlist);
		return "show";
	}
	@SuppressWarnings("unchecked")
	public String deleteProduct() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Set<OrderList> orderlist = (Set<OrderList>) session.getAttribute("orderlist");
		orderlist = productOrderService.deleteProduct(orderlist, pid);
		session.setAttribute("orderlist", orderlist);
		return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String saveProductOrder() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Users u = (Users) session.getAttribute("user");
		if (null == u)
			return LOGIN;
		Set<OrderList> orderlist = (Set<OrderList>) session.getAttribute("orderlist");
		if (null == orderlist){
			request.setAttribute("msg", "订单中没有任何产品，请选购喜欢的产品加入订单中。");
			return SUCCESS;
		}
		ProductOrder po = productOrderService.saveProductOrder(orderlist, u, ps);
		request.setAttribute("orderdetail", po);
		session.removeAttribute("orderlist");
		return SUCCESS;
	}
}
